package com.etc.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.etc.entity.Fix;
import com.etc.entity.Forum;
import com.etc.entity.jsonui.JsonUi;
import com.etc.service.FixService;
import com.etc.service.ForumService;
import com.etc.service.impl.FixServiceImpl;
import com.etc.service.impl.ForumServiceImpl;
import com.etc.util.PageData;
import com.google.gson.Gson;

/**
 * Servlet implementation class FixServlet
 */
@WebServlet("/fix.do")
public class FixServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private FixService fixService = new FixServiceImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public FixServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		//doQuery(request, response);
		
		String op = "query";
		if(request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		if(op.equals("query")) {
			doQuery(request, response);
		} else if(op.equals("del")) {
			
			int id = Integer.valueOf(request.getParameter("id"));
			
			boolean flag = fixService.DelFix(id);
			
			PrintWriter out = response.getWriter();
			
			out.print(flag);
			
			out.close();
		}else if(op.equals("update")) {
			BufferedReader reader = request.getReader();
			StringBuffer stringBuffer = new StringBuffer();
			String str = null;
			while((str=reader.readLine())!=null) {
				stringBuffer.append(str);
			}
			Fix data1 = new Gson().fromJson(stringBuffer + "", Fix.class);
			boolean flag = fixService.UpdFix(data1);
			System.out.println(data1);
			PrintWriter out = response.getWriter();
			out.print(flag);
//			out.close();
			System.out.println(flag);
		}
		else if(op.equals("removesession")) {
			//退出后清除所有的session信息
			request.getSession().invalidate();
			request.getRequestDispatcher("index.jsp").forward(request, response);

		}
	}

	private void doQuery(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
		int page = 1;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}

		int limit = 10;
		if (request.getParameter("limit") != null) {
			limit = Integer.parseInt(request.getParameter("limit"));
		}

		String strLike = "";
		if (request.getParameter("strLike") != null) {
			strLike = request.getParameter("strLike");
		}
		PageData<Fix> pd = fixService.GetfixAll(page, limit, strLike);

		JsonUi<Fix> jsonUi = new JsonUi<>(0, "", pd.getTotalCount(), pd.getData());

		Gson gson = new Gson();
		String string = gson.toJson(jsonUi);

		out.print(string);

		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
